<?php
session_start();
require 'connection.php';
require 'util.php';

if(isset($_POST['action']))
{
	try{
		$action = $_POST['action'];
		switch($action)
		{
			case "addUserImage":
				addUserImage();
				break;
			case "removeUserImage":
				removeUserImage();
				break;
			case "setUserImage":
				setUserImage();
				break;
			case "cropUserImage":
				cropUserImage();
				break;
			case "addResultImage":
				addResultImage();
				break;
			case "removeResultImage":
				removeResultImage();
				break;
			case "setResultImage":
				setResultImage();
				break;
			case "cropResultImage":
				cropResultImage();
				break;
		}
	}catch(PDOException $e){
		$response = array(
			"error" => true,
			"response" => $e->getMessage()
			);
		echo getJSON($response);
	}
}
function addUserImage()
{
	$imageType = 1;
	$pk = processUserPictures($_FILES, $imageType);
	$query = "SELECT * FROM user_picture WHERE pk = :pk";
	$params = array(
		":pk" =>  $pk
		);
	$result = getResultArray($query, getPdo(), $params)[0];
	$response = array(
		"pk" => $pk,
		"url" => $result['picture_url'],
		"cssClass" => "thumbClass"
		);
	echo getJSON($response);
}

function removeUserImage()
{
	$pk = $_POST['pk'];
	$params = array(
		":pk" => $pk
		);
	$result = getUserPictures(getLoggedInUser(), false, $pk);
	$imageReg = ($result != null && isset($result[0])) ? $result[0] : null;

	if($imageReg != null)
	{
		unlink($imageReg["picture_url"]);
		$croppedPictureUrl = trim(htmlspecialchars_decode($imageReg["cropped_picture_url"]));
		if($croppedPictureUrl != "")
		{
			unlink($croppedPictureUrl);
		}
		
	}
	executeDelete("user_picture", $params, getPdo());
	$response = array(
		"error" => false,
		"response" => "deleted"
		);
	echo getJSON($response);
}

function setUserImage()
{
	$pk = $_POST['pk'];

	$paramsResetMainPic = array(
		":main_picture"=> 0
		);
	$paramsWhereResetMainPic = array(
		":user_fk"=>getLoggedInUser()
		);
	executeUpdate("user_picture", $paramsResetMainPic, $paramsWhereResetMainPic, getPdo());

	$params = array(
		":main_picture" => 1
		);
	$paramsWhere = array(
		":pk" => $pk
		);
	executeUpdate("user_picture", $params, $paramsWhere, getPdo());
	$response = array(
		"error" => false,
		"response" => "updated"
		);
	echo getJSON($response);
}



function cropUserImage()
{
	$error = false;
	$message = "";

	$pk = $_POST['pk'];
	$url = $_POST['url'];
	$height = $_POST['height'];
	$width = $_POST['width'];
	$x = $_POST['x'];
	$y = $_POST['y'];
	$sufix = "_profile";

	//Delete already cropped image
	deleteCroppedImage("user_picture", $pk);
	$paramsCrop = array("x"=>$x, "y"=>$y, "width"=> $width, "height"=>$height);
	$finalCroppedPicture;
	if(cropImage($url, $sufix, $paramsCrop))
	{
		$croppedPictureUrl = addSufixFilePath($url, $sufix);

		$newUrl = getNewFilePath($url);
		$newCroppedPictureUrl = addSufixFilePath($newUrl, $sufix);
		rename($croppedPictureUrl, $newCroppedPictureUrl);
		$finalCroppedPicture = $newCroppedPictureUrl;
		$paramsSet = array(
		":cropped_picture_url"=>$newCroppedPictureUrl,
		":cropped"=>1
		);
		$paramsWhere = array(
			":pk"=>$pk
		);
		executeUpdate("user_picture", $paramsSet, $paramsWhere, getPdo());
		$error = false;
		$message = "The image was cropped successfully";
	}
	else
	{
		$error = true;
		$message = "Couldn't crop the image. Try again later.";
	}
	
	$response = array(
		"error"=>$error,
		"message" => $message,
		"cropped_picture_url"=>$finalCroppedPicture
		);
	echo getJSON($response);

	
}


//Listing
function addResultImage()
{
	$imageType = 1;
	$pk = processResultPictures($_FILES, $imageType);
	$query = "SELECT * FROM result_picture WHERE pk = :pk";
	$params = array(
		":pk" =>  $pk
		);
	$result = getResultArray($query, getPdo(), $params);
	if(!empty($result))
	{
		$result = $result[0];
	}
	
	$response = array(
		"pk" => $pk,
		"url" => $result['picture_url'],
		"cssClass" => "thumbClass"
		);
	echo getJSON($response);
}

function removeResultImage()
{
	$pk = $_POST['pk'];
	$params = array(
		":pk" => $pk
		);
	$result = getPictureRegister($pk, "result_picture");
	$imageReg = ($result != null && isset($result[0])) ? $result[0] : null;

	if($imageReg != null)
	{
		unlink($imageReg["picture_url"]);
		$croppedPictureUrl = trim(htmlspecialchars_decode($imageReg["cropped_picture_url"]));
		if($croppedPictureUrl != "")
		{
			unlink($croppedPictureUrl);
		}
		
	}
	executeDelete("result_picture", $params, getPdo());
	$response = array(
		"error" => false,
		"response" => "deleted"
		);
	echo getJSON($response);
}

function setResultImage()
{
	$pk = $_POST['pk'];

	$paramsResetMainPic = array(
		":main_picture"=> 0
		);
	$paramsWhereResetMainPic = array(
		":result_fk"=>getListingId()
		);
	executeUpdate("result_picture", $paramsResetMainPic, $paramsWhereResetMainPic, getPdo());

	$params = array(
		":main_picture" => 1
		);
	$paramsWhere = array(
		":pk" => $pk
		);
	executeUpdate("result_picture", $params, $paramsWhere, getPdo());
	$response = array(
		"error" => false,
		"response" => "updated"
		);
	echo getJSON($response);
}


function cropResultImage()
{
	$error = false;
	$message = "";

	$pk = $_POST['pk'];
	$url = $_POST['url'];
	$height = $_POST['height'];
	$width = $_POST['width'];
	$x = $_POST['x'];
	$y = $_POST['y'];
	$sufix = "_result";

	//Delete already cropped image
	deleteCroppedImage("result_picture", $pk);
	
	$paramsCrop = array("x"=>$x, "y"=>$y, "width"=> $width, "height"=>$height);
	$finalCroppedPicture;
	if(cropImage($url, $sufix, $paramsCrop))
	{
		$croppedPictureUrl = addSufixFilePath($url, $sufix);
		$newUrl = getNewFilePath($url);
		$newCroppedPictureUrl = addSufixFilePath($newUrl, $sufix);
		rename($croppedPictureUrl, $newCroppedPictureUrl);
		$finalCroppedPicture = $newCroppedPictureUrl;
		$paramsSet = array(
		":cropped_picture_url"=>$newCroppedPictureUrl,
		":cropped"=>1
		);
		$paramsWhere = array(
			":pk"=>$pk
		);
		executeUpdate("result_picture", $paramsSet, $paramsWhere, getPdo());
		$error = false;
		$message = "The image was cropped successfully";
	}
	else
	{
		$error = true;
		$message = "Couldn't crop the image. Try again later.";
	}
	
	$response = array(
		"error"=>$error,
		"message" => $message,
		"cropped_picture_url" => $finalCroppedPicture
		);
	echo getJSON($response);

	
}
?>